package com.three.service.impl;


import com.baor.baseUtil.R;
import com.three.dao.StudentDao;
import com.three.domain.Students;
import com.three.service.StudentService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public R updateStudent(Students students) {
        String plainPassword = students.getPassword();
        String hashedPassword = BCrypt.hashpw(plainPassword, BCrypt.gensalt());
        students.setPassword(hashedPassword);
        studentDao.updUser(students);
        return R.ok(studentDao.updateStudent(students));
    }
    //修改信息





    //查询在习生列表
    @Override
    public R studentsList(Integer pageIndex, Integer pageSize) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountstudents =studentDao.PageCountStudents();
        pageCountstudents = Math.ceil(pageCountstudents / pageSize);
        List<Students> studentsList = studentDao.studentsList(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountstudents", pageCountstudents);
        map.put("studentsList", studentsList);
        return R.ok(map);
    }
    //添加在习生
    @Override
    public R addstu(Map<String, Object> map) {
        String name = (String)map.get("name");
        String gender = (String) map.get("gender");
        String class_name = (String)map.get("class_name");
        String graduation_date = (String)map.get("graduation_date");
        String phone = (String)map.get("phone");
        String emergency_name = (String)map.get("emergency_name");
        String emergency_contact = (String)map.get("emergency_contact");
        String home = (String)map.get("home");
        System.out.println(name);
        int row = this.studentDao.addstudent(name, gender, class_name, graduation_date, phone, emergency_name, emergency_contact, home);
        studentDao.addstu(name,gender,phone);
        studentDao.updateUser_id(phone);
        if(row > 0) return R.ok();
        else return R.er();
    }
    //    修改更新在习生信息
    @Override
    public R updateStudents(Students student) {
        studentDao.updateUser(student);
        return R.ok(studentDao.updateStudents(student));
    }
    //软删除在习生
    @Override
    public int softDeleteStudent(int student_id,int user_id) {
        studentDao.softDeleteStudentByuser(user_id);
        return studentDao.softDeleteStudent(student_id);
    }
    //    关键字查询在习生列表
    @Override
    public  R findStudentByname(Integer pageIndex, Integer pageSize,String name) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountstukey =studentDao.PageCountstukey(pageIndex, pageSize,name);
        System.out.println(pageCountstukey);
        pageCountstukey = Math.ceil(pageCountstukey / pageSize);
        List<Students> stukeylist= studentDao.getStuByName(pageIndex, pageSize,name);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountstukey", pageCountstukey);
        map.put("stukeylist", stukeylist);
        return R.ok(map);
    }

}
